let m = 3, n = 3
const res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let num = 1;
let bottom = n - 1;
let top = 1;
let sum = n * n;
let x = 0, y = 0;
let part = 1;
while (num <= sum) {
  if (part === 1) {
    x = 0;
    y = 0;
  } else if (part % 2 === 0) {
    x = 0;
    y = top;
    top++;
  } else {
    x = bottom;
    y = 0;
    bottom--;
  }
  while (x < n && y < n) {
    res[x][y] = num;
    x++;
    y++;
    num++;
  }
  part++;
}
for (let k = 0; k < n; k++) {
  let str = res[k].join(' ');
  console.log(str);
}